<template>
	<view class="content">
		<view class="status_bar">
			<!-- 这里是状态栏 -->
		</view>
		<u-toast ref="uToast" />
		<!-- 搜索框 -->
		<view class="back">
			<view class="left" @tap="goback">
				<u-icon name="arrow-leftward" size="45"></u-icon>
			</view>
			<view class="right">
				<input @input="$u.debounce(getItemlist, 500)" type="text" v-model:value="searchValue"
					:placeholder="showKeyword" placeholder-style="color:#BDBDBD">
				<u-icon v-show="showKeyword !== ''" @click="clear" name="close"></u-icon>
			</view>
		</view>
		<!-- 模糊搜索展示的数据 -->
		<view class="u-popup" v-if="show" :style="{height:phoneheight}">
			<view class="pop-item" v-for="(item,index) in getsearch" :key="index">
				<u-icon color="#ccc" name="search"></u-icon>
				<view class="pop-itemname" @click="send(item.keyword)">
					<text>{{item.keyword}}</text>
				</view>
			</view>
		</view>

		<!-- 历史记录 -->
		<view class="history">
			<text class="history-text">历史记录</text>
			<view class="scroll">
				<view class="scroll-item" v-for="(item,index) in oldhistory" :key="index">
					<u-tag @click="UtagClick(item)" shape="circle " :text="item" :closeable="closeable"
						@close="tagClick(index)" />
				</view>
				<u-icon name="trash"></u-icon>
			</view>
		</view>
		<!-- 热搜榜 -->
		<view class="hot">
			<view class="hot-title">
				<text>热搜榜</text>
				<text>视频榜</text>
				<text>博客榜</text>
				<view class="hotplay">
					<u-icon name="play-right-fill" size="30"></u-icon>
					<text>播放</text>
				</view>
			</view>
			<view class="hotmsg" :style="{height:showhight + 'px'}">
				<view class="hotlist" v-for="(item,index) in hotlist" :key="index" @click="send(item.searchWord)">
					<view>
						<text :class="index < 3 ? 'first' : ''">
							{{index+ 1}}
						</text>
						<text>{{item.searchWord}}</text>
					</view>
				</view>
			</view>
			<view v-show="showhight !==290 " class="zhangkai" @click="zhankai">
				<text>展开更多热搜</text>
				<u-icon name="arrow-down" size="10"></u-icon>
			</view>
		</view>

		<view class="activity">
			<text class="activity-title">推荐活动</text>
			<view class="activity-img">
				<view>
					<image src="../../static/icon/activity1.jpg" mode=""></image>
					<text>邀请好友，得购物红包，共3重好礼！</text>
				</view>
			</view>
			<view class="activity-img">
				<view>
					<image src="../../static/icon/activity2.jpg" mode=""></image>
					<text>点击翻开你的一起听年度报告</text>
				</view>
			</view>
			<view class="activity-img">
				<view>
					<image src="../../static/icon/activity3.jpg" mode=""></image>
					<text>指尖上的神仙打架，你见过吗？</text>
				</view>
			</view>
			<view class="activity-img">
				<view>
					<image src="../../static/icon/activity4.jpg" mode=""></image>
					<text>喜提热评：走心评论送限定头像框</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getShowKeyword,
		getSuggest,
		getHotSearch,
		getmylist
	} from '../../network/search.js'
	export default {
		data() {
			return {
				searchmsg: '', //
				showKeyword: "", // 搜索框默认
				searchValue: "", // 搜索框的值
				getsearch: [], //搜索关键字
				show: false,
				closeable: true, //历史就关闭
				history: [], //历史记录
				oldhistory: [], //老的历史记录用于渲染新的
				hotlist: [], //热榜数据
				phoneheight: "",
				showhight: '150'
			}
		},
		
		onLoad() {
			this.getDfSearch();
			this._getHotSearch()
		},
		
		updated() {
			if (this.searchValue !== '') {
				this.show = true
			} else {
				this.show = false
			}
		},
		
		methods: {
			showToast() {
				this.$refs.uToast.show({
					title: '暂无数据',
					type: 'error',
				})
			},
			
			input() {
				console.log(123);
			}, //输入框获取焦点
			
			getDfSearch() {
				getShowKeyword(this.searchValue).then(res => {
					this.showKeyword = res.data.showKeyword
				})
			}, // 搜索框默认
			
			getItemlist() {
				if (this.searchValue !== '') {
					getSuggest(this.searchValue).then(res => {
						this.getsearch = res.result.allMatch
						// this.getsearch = res.result.allMatch
						if (res.result.allMatch === undefined) {
							this.showToast()
						}
					})
				}
			}, //获取模糊搜索的数据
			send(item) {
				getmylist(item).then(res => {
					this.$store.commit('getsearchlist', res.result.songs)
					uni.navigateTo({
						url: '/subpag/search2/search2?search'
					})
					this.$store.commit('getsearchname', item)
					this.oldhistory.push(item)
					let newcotent = Array.from(new Set(this.oldhistory))
					uni.setStorage({
						key: 'history',
						data: newcotent,
					});
					console.log(res);
				})
			},

			// 热搜榜
			_getHotSearch() {
				getHotSearch().then(res => {
					this.hotlist = res.data
				})
			},
			clear() {
				this.searchValue = ''
			}, //清除搜索框记录
			zhankai() {
				this.showhight = 290
			}, //展开所有
			goback() {
				uni.switchTab({
					url: "../../pages/discover/discover"
				})
			},
			async UtagClick(item) {
				this.send(item)
			},
			tagClick(index) {
				this.oldhistory.splice(index, 1)

				let newcotent = Array.from(new Set(this.oldhistory))
				uni.setStorage({
					key: 'history',
					data: newcotent,
				});
			}, // 移入关闭事件\



		}
	}
</script>

<style scoped lang="scss">
	// 搜索框
	.status_bar {
		height: var(--status-bar-height);
		width: 100%;
	}

	.back {
		display: flex;
		padding: 30rpx;
		align-items: center;

		.right {
			display: flex;
			align-items: center;
			justify-content: space-between;

			input {
				color: #BBBBBB;
			}

			border-bottom: 1px solid #BDBDBD;
			width: 100%;
			margin-left: 20rpx;
			padding: 10rpx 0;
		}
	}

	.content {
		position: relative;
		font-family: STXihei
	}

	.u-popup {
		width: 100%;
		display: flex;
		flex-direction: column;
		padding: 0 30rpx;
		z-index: 2;
		position: absolute;
		z-index: 2;
		background-color: #fff;

		.pop-item {
			height: 100rpx;
			display: flex;
			align-items: center;

			.pop-itemname {
				margin-left: 10rpx;
				border-bottom: 1px solid #F9F9F9;
				width: 100%;
			}
		}
	}

	//历史记录
	.history {
		padding: 0 30rpx;
		display: flex;
	}

	.scroll {
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}

	.scroll-item {
		margin: 5rpx;
	}

	.history-text {
		font-weight: lighter;
		margin-left: 10rpx;
	}

	//热搜榜单
	.hot {
		padding: 10rpx 30rpx;
	}

	.hot-title {
		display: flex;
		border-bottom: 1px solid #E6E6E6;
		padding-bottom: 10px;
		align-items: center;
		justify-content: space-between;
	}

	.hotplay {
		display: flex;
		align-items: center;
		border-radius: 20px;
		padding: 5rpx;
		border: 1px solid #ccc;
	}

	.hotlist {
		// align-items: center;
		width: 50%;

		view {
			display: flex;
			margin: 10rpx 0;

			.hotimage {
				width: 20px;
				height: 20px;
			}

			.texts {
				display: block;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}

			text {
				margin: 0 10rpx;

			}
		}
	}

	.hotmsg {
		width: 100%;
		display: flex;
		margin-top: 20rpx;
		flex-wrap: wrap;
		overflow: hidden;
		height: 300rpx;
		border: 1px solid #e5e5e5;
		border-radius: 10px;
		box-shadow: 0px 0px 1px 1px #e5e5e5;
	}

	.zhangkai {
		display: flex;
		align-items: center;
		justify-content: center;
		color: #ccc;
	}

	.activity {
		width: 100%;

		image {
			width: 80rpx;
			height: 80rpx;
			border-radius: 5px;
			margin-top: 10px;
			margin-left: 15px;
		}

		.activity-title {
			margin-left: 15px;
			font-family: "微软雅黑";
		}

		view {
			display: flex;

			// justify-content: center;
			// align-items: center;
			text {
				padding-top: 10px;
				margin-left: 10px;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 30rpx;
				overflow: hidden;
				text-overflow: ellipsis;


			}
		}

	}
</style>
